﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using TechCraftEngine;
using TechCraftEngine.Cameras;
using TechCraftEngine.Components;
using TechCraftEngine.Controllers;
using TechCraftEngine.Managers;
using TechCraftEngine.Particles;
using TechCraftEngine.WorldEngine;
using Blox.ParticleSystems;
using TechCraftEngine.Network;

namespace Blox.States
{
    public class PlayingState : State
    {
        private FirstPersonCamera _camera;
        private FirstPersonCameraController _cameraController;
        private ParticleManager _particleManager;
        private WeaponManager _weaponManager;

        public Player _player;
        private Texture2D _crosshairTexture;
        private Texture2D _underWaterTexture;

        private SpriteBatch _spriteBatch;
        private SpriteFont _spriteFont;
        private BlockPicker _blockPicker;

        private SkyDomeRenderer skyDomeRenderer;

        //public GameMouse m;


        public PlayingState()
        {
            //Game.IsMouseVisible = false;
            //Mouse.WindowHandle = Game.Window.Handle;
        }

        public override void Initialize()
        {
           // m = new GameMouse(null, Vector2.Zero, Global2.mgr);
            
            _camera = new FirstPersonCamera(Game);
            _camera.Initialize();
            _camera.Position = Vector3.Zero;
            _camera.LookAt(Vector3.Zero);
            _cameraController = new FirstPersonCameraController(Game);
            _cameraController.Initialize();
            Game.Camera = _camera;

            _player = new Player(Game, this, _game.GameClient.World, Global.PlayerPos);
            _player.Initialize();

            _spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            _blockPicker = new BlockPicker(Game, _spriteBatch);
            _blockPicker.Initialize();

            _particleManager = new ParticleManager(Game);
            BubbleParticleSystem pickupParticles = new BubbleParticleSystem(Game,Game.Content);
            pickupParticles.Initialize();
            _particleManager.ParticleSystems.Add(pickupParticles);


            _spriteFont = Game.Content.Load<SpriteFont>("Fonts\\console");

            _particleManager.ParticleEmitters.Add(new ParticleEmitter(_particleManager.ParticleSystems[0], 50, new Vector3(5,3,5)));
            _particleManager.ParticleEmitters.Add(new ParticleEmitter(_particleManager.ParticleSystems[0], 50, new Vector3(15,3, 15)));

            _weaponManager = new WeaponManager(_game, _game.GameClient.World, _player);
            _weaponManager.Initialize();

            skyDomeRenderer = new SkyDomeRenderer(Game.GraphicsDevice, _player.Camera, _player._world);
            skyDomeRenderer.Initialize();
        }

        public BlockPicker BlockPicker
        {
            get { return _blockPicker; }
        }

        public override void LoadContent()
        {
            _blockPicker.LoadContent();
            _player.LoadContent();
            _crosshairTexture = Game.Content.Load<Texture2D>("Textures\\crosshair");
            _underWaterTexture = Game.Content.Load<Texture2D>("Textures\\underwater");
            _weaponManager.LoadContent();
            skyDomeRenderer.LoadContent(Game.Content);
        }

        public override void ProcessInput(GameTime gameTime)
        {
            PlayerIndex controlIndex;
            if (_game.InputState.IsKeyPressed(Global.FireKey, null, out controlIndex) || _game.InputState.IsButtonPressed(Buttons.X,null, out controlIndex))
            {
                _weaponManager.Fire();
            }
            if (_game.InputState.IsKeyPressed(Global.FPSShowKey, null, out controlIndex) || _game.InputState.IsButtonPressed(Buttons.LeftShoulder, null, out controlIndex))
            {
                Global.ShowFPS = !Global.ShowFPS;
            }
            if (_game.InputState.IsKeyPressed(Keys.Escape, null, out controlIndex) ||
                _game.InputState.IsButtonPressed(Buttons.Back, null, out controlIndex))
            {
                Game.StateManager.ActiveState = Game.StateManager.GetState(typeof(MenuState));
            }
            /*if (_game.InputState.IsKeyPressed(Keys.T, null, out controlIndex))
            {
                ConsoleState st = (Game.StateManager.GetState(typeof(ConsoleState)) as ConsoleState);
                //st.Game = this.Game;
                //((ConsoleState)st).Game1 = this.Game;
                //Game.StateManager.ActiveState = Game.StateManager.GetState(typeof(ConsoleState));
                Game.StateManager.ActiveState = st;
            }*/
        }

        float prevTime = Global.TimeTimer;
        int prevSec = Global.TimeSeconds;
        int prevMinutes = Global.TimeMinutes;


        double timeLeft;

        public override void Update(GameTime gameTime)
        {
            //Mouse.SetPosition(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
            _cameraController.Update(gameTime);
            Game.Camera.Update(gameTime);
            _player.Update(gameTime);
            _game.GameClient.World.Update(gameTime);
            _blockPicker.Update(gameTime);
            _weaponManager.Update(gameTime);

            //m.Update();



            /*if (prevSec == 59)
            {
                Global.TimeMinutes++;
                prevMinutes = Global.TimeMinutes;

                Global.TimeSeconds = 0;
                prevSec = Global.TimeSeconds;
            }*/


            timeLeft += gameTime.ElapsedGameTime.TotalSeconds;
            const double triggerTime = 5;
            if (timeLeft > triggerTime)
            {
                timeLeft -= triggerTime;
                //Caret.Visible = !Caret.Visible;
                if (Global.TimeTimer == 23)
                {
                    Global.TimeTimer = 0;
                }
                else
                {
                    Global.TimeTimer++;
                }
            }

            _game.GameClient.World._tod = Global.TimeTimer;
          
            skyDomeRenderer.Update(gameTime);
         

            //Mouse.SetPosition(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
        }

        public override void Draw(GameTime gameTime)
        {
           

            _game.GraphicsDevice.Clear(Color.SkyBlue);

            skyDomeRenderer.Draw(gameTime);

            _game.GameClient.World.Draw(gameTime,_player.IsUnderWater);
            _player.Draw(gameTime);

            _weaponManager.Draw(gameTime);

           // _spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
            _spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend); 

            if (_player.IsUnderWater)
            {
                Rectangle screenRect = new Rectangle(0,0,_game.GraphicsDevice.Viewport.Width,_game.GraphicsDevice.Viewport.Height);
                _spriteBatch.Draw(_underWaterTexture, screenRect, Color.White);
            }
            _spriteBatch.Draw(_crosshairTexture, new Vector2(
                (Game.GraphicsDevice.Viewport.Width / 2) - 10,
                (Game.GraphicsDevice.Viewport.Height / 2) - 10), Color.White);
            _blockPicker.Draw(gameTime);

            

            _spriteBatch.End();   
         
            //m.Draw(_spriteBatch);
        }

    }
}
